package com.lxd.com;

import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.lxd.http.Http;
import com.lxd.http.HttpCallBack;
import com.lxd.http.HttpEngine;
import com.lxd.http.HttpRequest;


public class LXD_PictureScanActivity extends Activity implements OnClickListener,HttpCallBack{
    /** Called when the activity is first created. */
	
	private Button btn;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		 HttpEngine engine = new HttpEngine();
        HttpRequest request = new HttpRequest("http://sbnnest.com/picture/1.jpg");
        request.setHttpCallBack(this);
        engine.addRequest(request);
	        
	}

	@Override
	public void onError(int requestId, int errCode, byte[] errStr, Http http) {
		// TODO Auto-generated method stub
		
	}

	final Handler handler = new Handler() {  
         public void handleMessage(Message message) {  
         System.out.println("handleMessage");	
        	 btn.setBackgroundDrawable(new BitmapDrawable(bm));
         }  
    }; 
    
    Bitmap bm;
	@Override
	public void onReceived(int requestId, byte[] data, Http http) {
		// TODO Auto-generated method stub
		System.out.println("onReceived:" + new String(data));
		bm=BitmapFactory.decodeByteArray(data, 0, data.length);
         handler.sendEmptyMessage(0); 
	}

	@Override
	public void onReceived(int requestId, InputStream stream,
			long contentLength, Http http) {
		// TODO Auto-generated method stub
		
	}
}